package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.FriendRequest;
import com.ruoyi.system.domain.vo.FriendRequestVO;
import com.ruoyi.system.domain.vo.SessionVO;
import com.ruoyi.system.service.IFriendRequestService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Session;
import com.ruoyi.system.service.ISessionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会话Controller
 * 
 * @author ruoyi
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/system/session")
public class SessionController extends BaseController<ISessionService, Session, SessionVO> {

    @Autowired
    private ISessionService sessionService;



    /**
     *  更新未读消息数
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/updateSessionVOInfo")
    public AjaxResult updateSessionVOInfo(@RequestBody SessionVO sessionVO) {
        sessionService.updateSessionVOInfo(sessionVO);
        return success();
    }


    /**
     * 设置用户当前所处会话
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/setSessionSelectedStatus")
    public AjaxResult setSessionSelectedStatus(@RequestBody SessionVO sessionVO) {
        sessionService.setSessionSelectedStatus(sessionVO);
        return success();
    }


    /**
     * 查询会话列表
     *      - 初始化用户所处会话信息
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/querySessionList")
    public AjaxResult querySessionList(@RequestBody SessionVO sessionVO) throws Exception {
        List<SessionVO> result = sessionService.querySessionList(sessionVO);
        return success(result);
    }


    /**
     * 创建会话
     */
    @PreAuthorize("@ss.hasPermi('system:chat:list')")
    @PostMapping("/createChatSession")
    public AjaxResult createChatSession(@RequestBody SessionVO sessionVO) {
        boolean result = sessionService.createChatSession(sessionVO);
        return success(result);
    }


    /**
     * 查询会话列表
     */
    @PreAuthorize("@ss.hasPermi('system:session:list')")
    @GetMapping("/list")
    public TableDataInfo list(Session session)
    {
        startPage();
        List<Session> list = sessionService.selectSessionList(session);
        return getDataTable(list);
    }

    /**
     * 导出会话列表
     */
    @PreAuthorize("@ss.hasPermi('system:session:export')")
    @Log(title = "会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Session session)
    {
        List<Session> list = sessionService.selectSessionList(session);
        ExcelUtil<Session> util = new ExcelUtil<Session>(Session.class);
        util.exportExcel(response, list, "会话数据");
    }

    /**
     * 获取会话详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:session:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sessionService.selectSessionById(id));
    }

    /**
     * 新增会话
     */
    @PreAuthorize("@ss.hasPermi('system:session:add')")
    @Log(title = "会话", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Session session)
    {
        return toAjax(sessionService.insertSession(session));
    }

    /**
     * 修改会话
     */
    @PreAuthorize("@ss.hasPermi('system:session:edit')")
    @Log(title = "会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Session session)
    {
        return toAjax(sessionService.updateSession(session));
    }

    /**
     * 删除会话
     */
    @PreAuthorize("@ss.hasPermi('system:session:remove')")
    @Log(title = "会话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sessionService.deleteSessionByIds(ids));
    }
}
